<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.store_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
        <template slot-scope="scope" slot="menu">
          <el-button
            type="text"
            icon="el-icon-paperclip"
            @click.stop="approval(scope.row)"
          >审批
          </el-button>
        </template>
      <template slot-scope="{row}"
                slot="storeName">
        <el-tag>{{row.storeName}}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="isApproval">
        <el-tag>{{row.isApproval}}</el-tag>
      </template>
    </avue-crud>
    <template>
      <el-dialog title="审批" :visible.sync="dialogFormVisible">
        <el-form :model="forms">
          <el-form-item label="是否通过：" :label-width="formLabelWidth">
              <el-radio v-model="forms.approval" label="1">通过</el-radio>
              <el-radio v-model="forms.approval"  label="2">不通过</el-radio>
          </el-form-item>
          <el-form-item label="原因：" :label-width="formLabelWidth">
            <el-input
              type="textarea"
              placeholder="请输入内容"
              v-model="forms.reason"
              maxlength="30"
              show-word-limit
            >
            </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="determine">确 定</el-button>
        </div>
      </el-dialog>
    </template>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove,updateApproval} from "@/api/store/store";
  import {isvalidatemobile} from "@/util/validate";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        dialogFormVisible:false,
        storeData:{},
        form: {},
        forms: {
          approval: '',
          reason:'',
        },
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "商店名称",
              prop: "storeName",
              slot: true,
              search: true,
              rules: [{
                required: true,
                message: "请输入商店名称",
                trigger: "blur"
              }]
            },
            {
              label: "商店图像",
              prop: "img",
              propsHttp: {
                res: 'data'
              },
              type: 'upload',
              listType: 'picture-img',
              tip: '只能上传jpg/png用户头像，且不超过500kb',
              action: '/api/pxx_commodity/commodity/upload',
            },
            {
              label: "手机号",
              prop: "phone",
              rules: [{
                required: true,
                message: "请输入商店手机号",
                trigger: "blur"
              }]
            },
            {
              label: "评分",
              prop: "score",
              addDisplay: false,
              editDisplay: false,
            },
            {
              label: "审批状态",
              prop: "isApproval",
              slot: true,
              addDisplay: false,
              editDisplay: false,
            },
            {
              label: "原因",
              prop: "reason",
              hide:true,
              addDisplay: false,
              editDisplay: false,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.store_add, false),
          viewBtn: this.vaildData(this.permission.store_view, false),
          delBtn: this.vaildData(this.permission.store_delete, false),
          editBtn: this.vaildData(this.permission.store_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      cancel(){
        this.forms = {};
        this.dialogFormVisible = false;
      },
      determine(done, loading){
        var row = this.storeData;
        row.approval = this.forms.approval;
        row.reason = this.forms.reason;
        updateApproval(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
        this.forms = {};
        this.dialogFormVisible = false;
        location.reload();
      },
      approval(row){
        this.dialogFormVisible = true;
        this.storeData = row;
      },
      rowSave(row, done, loading) {
        if (row.img=== undefined){
          done();
          this.$message.error("商店没有图片!");
          return;
        }
        var list = isvalidatemobile(row.phone);
        if (list.result == false) {
          done();
          this.$message.error(list.msg);
          return;
        }
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        if (row.img=== undefined){
          done();
          this.$message.error("商店没有图片!");
          return;
        }
        var list = isvalidatemobile(row.phone);
        if (list.result == false) {
          done();
          this.$message.error(list.msg);
          return;
        }
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        console.log(row)
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
